﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace PocoReport
{
    public static class XElementExtentions
    {
        public static string GetAttribValue(this XElement element, string name, string defaultValue)
        {
            XAttribute attribute = element.Attribute(name);
            return attribute == null ? defaultValue : attribute.Value;
        }

        public static void SetAttribValue(this XElement element, string name, object value)
        {
            string stringValue = null;
            if (value != null)
            {
                stringValue = value.ToString();
            }

            if (!string.IsNullOrEmpty(stringValue))
            {
                element.SetAttributeValue(name, stringValue);
            }
            else
            {
                XAttribute attribute = element.Attribute(name);
                if(attribute != null)
                {
                    attribute.Remove();
                }
            }
        }
    }
}
